package com.zc.zby.basicframedemo.activity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.zc.zby.basicframedemo.R;
import com.zc.zby.basicframedemo.adapter.Progress2Adapter;
import com.zc.zby.basicframedemo.adapter.Progress3Adapter;
import com.zc.zby.basicframedemo.adapter.ProgressAdapter;
import com.zc.zby.basicframedemo.base.RxBaseActivity;
import com.zc.zby.basicframedemo.model.HuanBaoItemModel;
import com.zc.zby.basicframedemo.model.ProgressModel;
import com.zc.zby.basicframedemo.weight.FullyGridLayoutManager;
import com.zc.zby.basicframedemo.weight.FullyLinearLayoutManager;

import java.util.ArrayList;

import butterknife.BindView;

public class ProgressActivity extends RxBaseActivity {
    @BindView(R.id.easyRecyclerView)
    protected EasyRecyclerView mEasyRecyclerView;
    @BindView(R.id.easyRecyclerView2)
    protected EasyRecyclerView mEasyRecyclerView2;
    @BindView(R.id.easyRecyclerView3)
    protected EasyRecyclerView mEasyRecyclerView3;
    @BindView(R.id.iv_huanbao_bg)
    protected ImageView mIvHuanBaoBg;
    @BindView(R.id.combinedChart)
    protected CombinedChart mCombinedChart;
    private ProgressAdapter progressAdapter;
    private ArrayList<ProgressModel> progressList, progress2List;
    private ArrayList<HuanBaoItemModel> progress3List;
    private Progress2Adapter progress2Adapter;
    private Progress3Adapter progress3Adapter;
    private String[] mHuanBaoTitles = {"PM2.5", "PM10", "SO2", "NO2", "CO", "O3"};
    private String[] mHuanBaoValues = {"14", "45", "9", "36", "8", "75"};
    private Typeface tfLight;
    private String[] months = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

    @Override
    public int getLayoutId() {
        return R.layout.activity_progress;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        mEasyRecyclerView.setLayoutManager(new FullyLinearLayoutManager(this));
        mEasyRecyclerView.setAdapter(progressAdapter = new ProgressAdapter(this));
        mEasyRecyclerView2.setLayoutManager(new FullyGridLayoutManager(this, 2));
        mEasyRecyclerView2.setAdapter(progress2Adapter = new Progress2Adapter(this));
        mEasyRecyclerView3.setLayoutManager(new FullyGridLayoutManager(this, 6));
        mEasyRecyclerView3.setAdapter(progress3Adapter = new Progress3Adapter(this));
        ViewGroup.LayoutParams layoutParams = mIvHuanBaoBg.getLayoutParams();
        layoutParams.height = layoutParams.width * 38 / 63;
        mIvHuanBaoBg.setLayoutParams(layoutParams);
        initChart();
    }

    private void initChart() {
        tfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
        mCombinedChart.getDescription().setEnabled(false);
        mCombinedChart.setPinchZoom(false);
        // 设置为false以禁止通过在其上双击缩放图表。
        mCombinedChart.setDoubleTapToZoomEnabled(false);
        // 启用/禁用缩放在y轴。
        mCombinedChart.setScaleYEnabled(false);
        // 启用/禁用拖动（平移）图表
        mCombinedChart.setDragEnabled(true);

        // draw bars behind lines
        mCombinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE
        });

        Legend l = mCombinedChart.getLegend();
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setTextSize(12f);
        l.setEnabled(false);

        YAxis rightAxis = mCombinedChart.getAxisRight();
        rightAxis.setDrawGridLines(false);
        rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);

        YAxis leftAxis = mCombinedChart.getAxisLeft();
        leftAxis.setDrawGridLines(true);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);

        XAxis xAxis = mCombinedChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
//        xAxis.setGranularity(0.5f);
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                if (value < 0 || value > (months.length - 1)) {
                    return "";
                }
                return months[(int) value % months.length];
            }
        });

        CombinedData data = new CombinedData();
        data.setData(generateLineData());
        data.setData(generateBarData());
        mCombinedChart.setData(data);
        //使得两侧柱子完全显示
        xAxis.setAxisMinimum(mCombinedChart.getCombinedData().getXMin() - 1f);
//        xAxis.setAxisMaximum(mCombinedChart.getCombinedData().getXMax() + 1f);
        mCombinedChart.animateX(1500);//数据显示动画，从左往右依次显示
        mCombinedChart.invalidate();
    }

    private LineData generateLineData() {
        LineData d = new LineData();
        ArrayList<Entry> entries = new ArrayList<>();
        for (int index = 0; index < months.length; index++)
            entries.add(new Entry(index + 0.5f, getRandom(15, 5)));

        LineDataSet set = new LineDataSet(entries, "增速（%）");
        set.setColor(Color.rgb(253, 137, 8));
        set.setLineWidth(2f);
        set.setCircleColor(Color.rgb(253, 137, 8));//设置圆圈的颜色
        set.setCircleRadius(3f);
        set.setFillColor(Color.WHITE);//设置圆圈内部洞的颜色
        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set.setDrawValues(false);
        set.setValueTextSize(10f);
        set.setValueTextColor(Color.rgb(240, 238, 70));
        d.addDataSet(set);
        return d;
    }

    private BarData generateBarData() {

        ArrayList<BarEntry> entries1 = new ArrayList<>();

        for (int index = 0; index < months.length; index++) {
            entries1.add(new BarEntry(index, getRandom(25, 25)));
        }

        BarDataSet set1 = new BarDataSet(entries1, "投资目标（万元）");
        set1.setColor(Color.rgb(132, 192, 255));
        set1.setValueTextSize(10f);
        BarData d = new BarData(set1);
        d.setDrawValues(false);
        return d;
    }

    protected float getRandom(float range, float start) {
        return (float) (Math.random() * range) + start;
    }

    @Override
    public void setListeners() {
    }

    @Override
    public void initToolBar() {

    }

    @Override
    public void initData() {
        progressList = new ArrayList<>();
        for (int i = 2; i > 0; i--) {
            ProgressModel progressModel = new ProgressModel();
            progressModel.currentValue = i * 20;
            progressList.add(progressModel);
        }
        progressAdapter.addAll(progressList);
        progress2List = new ArrayList<>();
        for (int i = 4; i > 0; i--) {
            ProgressModel progressModel = new ProgressModel();
            progressModel.currentValue = i * 20;
            switch (i) {
                case 1:
                    progressModel.startColor = getResources().getColor(R.color.progress_start1);
                    progressModel.endColor = getResources().getColor(R.color.progress_end1);
                    break;
                case 2:
                    progressModel.startColor = getResources().getColor(R.color.progress_start2);
                    progressModel.endColor = getResources().getColor(R.color.progress_end2);
                    break;
                case 3:
                    progressModel.startColor = getResources().getColor(R.color.progress_start3);
                    progressModel.endColor = getResources().getColor(R.color.progress_end3);
                    break;
                case 4:
                    progressModel.startColor = getResources().getColor(R.color.progress_start4);
                    progressModel.endColor = getResources().getColor(R.color.progress_end4);
                    break;
            }
            progress2List.add(progressModel);
        }
        progress2Adapter.addAll(progress2List);

        progress3List = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            HuanBaoItemModel huanBaoItemModel = new HuanBaoItemModel();
            huanBaoItemModel.title = mHuanBaoTitles[i];
            huanBaoItemModel.value = mHuanBaoValues[i];
            progress3List.add(huanBaoItemModel);
        }
        progress3Adapter.addAll(progress3List);
    }
}
